package mywebapps.jsf;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import mywebapps.beans.ImagesBean;
import mywebapps.beans.ImagesBean.ImageType;

@Named("showImageBean")
@SessionScoped
public class ShowImageAction implements Serializable {
	private static final long serialVersionUID = 8099670178694789663L;
	
	private boolean imageDisplayed;
	private String currentImageName;
	private ImageType currentImageType;
	
	@Inject private ImagesBean imagesBean;
	
	@PostConstruct
	public void init() {
		imageDisplayed = false;
	}
	
	public void setImagesBean(ImagesBean imagesBean) {
		this.imagesBean = imagesBean;
	}
	
    public boolean isImageDisplayed() {
    	return imageDisplayed;
    }
    
    public String getCurrentImageName() {
    	return currentImageName;
    }
    
    /***** ACTIONS *****/
    
    public void displayImageAction(String file, String imageType) {
    	currentImageName = file;
    	currentImageType = ImageType.valueOf(imageType);
    	imageDisplayed = true;
    }
    
    public void hideImageAction() {
    	currentImageName = null;
    	imageDisplayed = false;
    }
    
    public void showNextImage() {
    	String[] imageNames = null;
    	if(currentImageType == ImageType.PAINTING) {
    	    imageNames = imagesBean.getPaintings();
    	} else
		if(currentImageType == ImageType.SKETCH) {
    	    imageNames = imagesBean.getSketches();
    	}
    	
    	for(int i = 0; i < imageNames.length; i++) {
    		if(imageNames[i].compareToIgnoreCase(currentImageName) == 0) {
    			currentImageName = imageNames[(i+1) % imageNames.length];
    			break;
    		}
    	}
    }
    
    public void showPreviousImage() {
    	String[] imageNames = null;
    	if(currentImageType == ImageType.PAINTING) {
    	    imageNames = imagesBean.getPaintings();
    	} else
		if(currentImageType == ImageType.SKETCH) {
    	    imageNames = imagesBean.getSketches();
    	}
    	
    	for(int i = imageNames.length-1; i >= 0; i--) {
    		if(imageNames[i].compareToIgnoreCase(currentImageName) == 0) {
    			if(i-1 < 0) {
    				currentImageName = imageNames[imageNames.length - 1];
    			} else {
    			    currentImageName = imageNames[i-1];
    			}
    			break;
    		}
    	}
    }
}
